/** 
 * 敌人组件
 * 包括移动速度方向
 * 功能 敌人自我移动
*/
cc.Class({
    extends: cc.Component,
    properties: {
       
        weight: {//未使用
                 // ATTRIBUTES:
                 default: 0,        // The default value will be used only when the component attaching
                 type: cc.Integer, // optional, default is typeof default
                 serializable: true,   // optional, default is true
             },
        weightLabel:{
              // ATTRIBUTES:
              default: null,        // The default value will be used only when the component attaching
              type: cc.Label,// optional, default is typeof default
              serializable: true,   // optional, default is true
        },
        speed: {//移动速度
                // ATTRIBUTES:
                default: 0,        // The default value will be used only when the component attaching
                type: cc.Float, // optional, default is typeof default
                serializable: true,   // optional, default is true
        },
        
        collideType:{//表示
                 default: 0,        // The default value will be used only when the component attaching
                 type: cc.Integer, // optional, default is typeof default
                 serializable: true,
        },

        dir: cc.v2(0, 0),//的移动方向

        camera :{//相机
                default: null,
                type: cc.Node,
                serializable: false,
        },
        trail:{//移动速度
            // ATTRIBUTES:
            default: 0,        // The default value will be used only when the component attaching
            type: cc.Float, // optional, default is typeof default
            serializable: true,   // optional, default is true
        },
    },
    
    Random :function() {//随机初始化
        this.weight = Math.random()*10000;
        this.speed = Math.random()*10;
        this.dir.x = (Math.random()-0.5)*2;
        this.dir.y =Math.random();
   
    },
    initData:function(scale,x,y, weight,speed,dirX,trail)
    {
          this.trail = trail; 
        
        if(typeof scale == typeof [])
        {
            this.node.scale = scale[0] + (scale[1]-scale[0])*Math.random();
        }else if(scale == -1)
        {
            this.node.scale = 0.3 + Math.random();
        }else
        {
            this.node.scale = scale;
        }
        
    


        if(typeof x == typeof [])
        {
            this.node.x = x[0]+ (x[1]-x[0])*Math.random();
        }else if (x == -1)
        {
            this.node.x = Math.random()*640 ;
        }else
        {
            this.node.x = x;
        }
        

        if(typeof speed == typeof [])
        {
            this.speed = speed[0] + (speed[1]-speed[0])*Math.random();
        }else if (speed == -1)
        {
            this.speed = Math.random()*5 ;
        }else
        {
            this.speed = speed;
        }

        this.node.y = y;
        if(typeof weight == typeof [])
        {
            var wei =   weight[0] + Math.floor( (weight[1]-weight[0])*Math.random());
            this.weight = wei;
         //   this.weightLabel.string = ""+wei;
        }else if(weight == -1 )
        {
            weight =Math.floor( Math.random()*10000);
            this.weight = weight;
        //    this.weightLabel.string = ""+weight;
        }else
        {
            this.weight = weight;
         //   this.weightLabel.string = ""+weight;
        }



        if(typeof dirX == typeof [])
        {
            var dirO =  dirX[0] + (dirX[1]-dirX[0])*Math.random();
            var yy =- Math.sin(dirO*3.14/180.0);
            var xx = Math.cos(dirO*3.14/180);
            this.dir.x = xx;
            this.dir.y = yy;

        }else if(dirX  == -1  )
        {
            var dirO = 3.14*Math.random();
            var yy =- Math.sin(dirO);
            var xx = Math.cos(dirO);
            this.dir.x = xx;
            this.dir.y = yy;
        }else
        {
            var dirO =  dirX;
            var yy =- Math.sin(dirO*3.14/180.0);
            var xx = Math.cos(dirO*3.14/180);
            this.dir.x = xx;
            this.dir.y = yy;
        }
        
        
        
       

      
       

       // this.Random();
    },
    setNode(nd)//设置相机
    {
        this.camera =nd;
    },

    //敌人单位自动更新
    update: function (dt) {


        if(this.dir.y >= 0)
        {
            this.dir.y = -this.dir.y ;
        }

        if(this.dir.y > -0.1)
        {
            //dirxthis.dir.y = -0.1;
        }
        
      

        this.dir = this.dir.normalize();


        this.node.x += this.dir.x * this.speed * dt*60;
        this.node.y += this.dir.y * this.speed * dt*60 ;


        if(this.node.x < 0 && this.trail==1)
        {
            this.node.x = -this.node.x;

            this.dir.x = - this.dir.x;
        }

        if(this.node.x > 640 && this.trail ==1)
        {
            this.node.x = 640;

            this.dir.x = - this.dir.x;
        }


        if(  this.node.y <this.camera.y - 500)
        {
            this.node.destroy();
        }        
        
    },
    

    
});
